package icu.zlz.common.core.utils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import java.security.Security;
import java.util.Base64;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class AESUtil {
    //算法名称
    private static final String ALGORITHM = "AES";
    private static final String TRANSFORMATION = "AES";


    static {
        // 添加 BouncyCastle 提供者
        Security.addProvider(new BouncyCastleProvider());
    }

    public static void main(String[] args) throws Exception {
        SecretKey secretKey = generateAESKey(128);

        // 将密钥转换为Base64编码的字符串
        String encodedKey = Base64.getEncoder().encodeToString(secretKey.getEncoded());

        System.out.println("Generated 128-bit AES Key: " + encodedKey);

        System.out.println(decrypt("m4YISbAZB+K+vrwD/6wQ2Q==",encrypt("m4YISbAZB+K+vrwD/6wQ2Q==","asdafas")));
        System.out.println(encrypt("m4YISbAZB+K+vrwD/6wQ2Q==","asdafasasdafasasdafasasdafasasdafas"));
    }

    public static SecretKey generateAESKey(int keySize) throws Exception {
        // 获取AES密钥生成器实例
        KeyGenerator keyGen = KeyGenerator.getInstance("AES", "BC");

        // 初始化密钥生成器，指定密钥长度
        keyGen.init(keySize);
        // 生成密钥
        return keyGen.generateKey();
    }

    public static String encrypt(String key, String data) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(Base64.getDecoder().decode(key), ALGORITHM);
        Cipher cipher = Cipher.getInstance(TRANSFORMATION, "BC");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedData = cipher.doFinal(data.getBytes());
        return Base64.getEncoder().encodeToString(encryptedData);
    }

    public static String decrypt(String key, String encryptedData) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(Base64.getDecoder().decode(key), ALGORITHM);
        Cipher cipher = Cipher.getInstance(TRANSFORMATION, "BC");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
        return new String(decryptedData);
    }

//    //加密
//    public static String encrypt(String key,String data) throws Exception {
//        SecretKeySpec secretKey = new SecretKeySpec(Base64.getDecoder().decode(key), ALGORITHM);
//        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
//        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
//        byte[] encryptedData = cipher.doFinal(data.getBytes());
//        return Base64.getEncoder().encodeToString(encryptedData);
//    }
//    // 解密
//    public static String decrypt(String key,String encryptedData) throws Exception {
//        SecretKeySpec secretKey = new SecretKeySpec(Base64.getDecoder().decode(key), ALGORITHM);
//        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
//        cipher.init(Cipher.DECRYPT_MODE, secretKey);
//        byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
//        return new String(decryptedData);
//    }



}
